package cn.luckdou.system.service.impl.country;

import cn.luckdou.commons.utils.DictUtils;
import cn.luckdou.commons.utils.StringUtils;
import cn.luckdou.system.mapper.country.CountryMapper;
import cn.luckdou.system.model.country.ContinentVo;
import cn.luckdou.system.model.country.Country;
import cn.luckdou.system.model.country.CountryVo;
import cn.luckdou.system.model.country_details.CountryDetails;
import cn.luckdou.system.mapper.country_details.CountryDetailsMapper;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import cn.luckdou.system.service.country.ICountryService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 国家 服务实现类
 * </p>
 *
 * @author Dadou
 * @since 2017-08-11
 */
@Service
public class CountryServiceImpl extends ServiceImpl<CountryMapper, Country> implements ICountryService {
    @Autowired
    private CountryMapper countryMapper;
    @Autowired
    private CountryDetailsMapper countryDetailsMapper;

    @Transactional
    public void insert(Country country, CountryDetails countryDetails) {
        Integer insert = countryMapper.insert(country);
        if (insert!=null && insert>0){
            countryDetails.setCountryId(country.getId());
            Integer insert1 = countryDetailsMapper.insert(countryDetails);
            if (insert1!=null && insert1>0){
                return ;
            }
        }
        throw  new RuntimeException();
    }

    @Transactional
    public void updateById(Country country, CountryDetails countryDetails) {
        Integer integer = countryMapper.updateById(country);
        if (integer!=null && integer>0){
            if (countryDetails.getId()!=null){
                Integer integer1 = countryDetailsMapper.updateById(countryDetails);
                if(integer1!=null && integer1>0){
                    return;
                }
            }else{
                Integer insert1 = countryDetailsMapper.insert(countryDetails);
                if(insert1!=null && insert1>0){
                    return;
                }
            }
        }
        throw  new RuntimeException();
    }

    @Override
    @Transactional
    public boolean deleteById2(Long id) {
        Integer integer = countryMapper.deleteById(id);
        if (integer!=null && integer>0){
            List<CountryDetails> countryDetailsList = countryDetailsMapper.selectList(new EntityWrapper<CountryDetails>().eq("country_id", id));
            if (countryDetailsList!=null && countryDetailsList.size()>0){
                Integer integer1 = countryDetailsMapper.deleteById(countryDetailsList.get(0));
                if( integer1!=null) return true;
            }
        }

        return false;
    }


    /**
     * 下拉框的初始化 以及模糊搜索
     * @param q
     * @return
     */
    public List<Country> selectAllListByParamter(String q){
        if(StringUtils.isNotBlank(q)){
            List<Country> countries = countryMapper.selectAllListByParamter(q);
            return  countries;
        }else{
            return  countryMapper.selectAllList();
        }
    }

    /**
     * 国家下来框的初始化
     * @param q
     * @return
     */
    public List<ContinentVo> getcountryList(String q) {
        List<ContinentVo> list = new ArrayList<>();
        List<Country> countries = new ArrayList<>();
        if (StringUtils.isNotBlank(q)) {
            countries = countryMapper.selectAllListByParamter(q);
        } else {
            countries = countryMapper.selectAllList();
        }
        for (Country country : countries) {
            int flag = 0;
            for (ContinentVo continentVo : list) {
                if (continentVo.getId().equals("c" + country.getType().toString())) {
                    flag = 1;
                    List<CountryVo> children = continentVo.getChildren();
                    children.add(new CountryVo(country.getId().toString(), country.getCaname()));
                }
            }
            if (flag == 0) {
                //添加洲别 添加children
                ContinentVo continentVo = new ContinentVo("c" + country.getType().toString(), "closed");
                List<CountryVo> children = new ArrayList<>();
                children.add(new CountryVo(country.getId().toString(), country.getCaname()));
                continentVo.setChildren(children);
                list.add(continentVo);
            }
        }

        for (ContinentVo continentVo : list) {
            continentVo.setText(DictUtils.getBusDicLabel("COUNTRY_TYPE", continentVo.getId().substring(1), "") + "(" + continentVo.getChildren().size() + ")");
        }
        return list;
    }

}
